﻿/*
* jxymnet 系列 jquery插件 之 tabs
* 作者：jxymnet jxymnet@163.com http://blog.jxymnet.cn
* 日期：2011-06-12
* 支持同页面多个tabs对象,支持3种动画切换 show，slide，fade,none
*/
(function ($) {
    $.fn.extend({
        jxymTree: function (config) {
            //默认配置
            var defaultOption = {
                thisPath: "script/jQuery/jxymTree", /*插件根目录*/
                skinName: "skin1", /*样式*/
                animateType: "show", /*切换动画*/
                isCookie: true, /*是否启用cookie记忆[需要jquery.cookie.js]*/
                animateTime: 200 /*动画延时*/
            };
            //注册新配置
            var options = $.extend(defaultOption, config);

            /*定义展开折叠和节点的图标状态*/
            var imgOpenedURL = options.thisPath + "/styles/" + options.skinName + "/opened.gif";
            var imgClosedURL = options.thisPath + "/styles/" + options.skinName + "/closed.gif";
            var imgChildURL = options.thisPath + "/styles/" + options.skinName + "/child.gif";
            //遍历tabs对象
            $(this).each(function (treeIndex) {
                var _tree = $(this);
                var _ids = "";
                $(this).addClass("jxymTree");
                /*隐藏tree下的所有子节点*/
                $("ul", _tree).css({ 'display': 'none' });
                /*设置所有不含有子节点的节点child状态*/
                $("li", _tree).filter(function (index) {
                    return $("ul", this).length == 0;
                }).prepend("<img src=\"" + imgChildURL + "\" class=\"sc\" />");

                $("li", _tree).each(function (i) {
                    $(this).attr({ "id": "jxymTree_" + treeIndex + "_items_" + i });
                });

                $("li>a", _tree).click(function () {
                    $(this).addClass("action");
                    $.cookie("jxymTreeAction", $(this).parents("li").attr("id"));
                });

                $("li", _tree).filter(function (index) {
                    return $("ul", this).length > 0;
                }).prepend("<img src=\"" + imgClosedURL + "\" alt=\"展开\" title=\"展开\" class=\"s\" />").find("img.s").click(function () {
                    if (options.isCookie) {
                        if ($(this).attr("alt") == "展开") {
                            saveNodeCookie(this, true);
                        } else {
                            saveNodeCookie(this, false);
                        }
                    }
                    /*设置所有含有子节点的节点closed状态并且绑定切换展开、折叠的事件*/
                    switch (options.animateType) {
                        default:
                            if ($(this).attr("alt") == "展开") {
                                $(this).attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" }).siblings("ul").show();
                                $(this).parent().siblings().find("img.s").attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide();
                            } else {
                                $(this).attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide();
                            }
                            break;
                        case "show":
                            if ($(this).attr("alt") == "展开") {
                                $(this).attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" }).siblings("ul").show(options.animateTime);
                                $(this).parent().siblings().find("img.s").attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide(options.animateTime);
                            } else {
                                $(this).attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide(options.animateTime);
                            }
                            break;
                        case "fade":
                            if ($(this).attr("alt") == "展开") {
                                $(this).attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" }).siblings("ul").fadeIn(options.animateTime);
                                $(this).parent().siblings().find("img.s").attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").fadeOut(0);
                            } else {
                                $(this).attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").fadeOut(0);
                            }
                            break;
                        case "slide":
                            if ($(this).attr("alt") == "展开") {
                                $(this).attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" }).siblings("ul").slideDown(options.animateTime);
                                $(this).parent().siblings().find("img.s").attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").slideUp(options.animateTime);
                            } else {
                                $(this).attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").slideUp(options.animateTime);
                            }
                            break;
                    }
                });
            });
            /*cookie展开节点*/
            if (options.isCookie) {
                var jxymTreeOpenedId = $.cookie('jxymTreeOpenedId');
                var jxymTreeisOpen = $.cookie('jxymTreeisOpen');
                var jxymTreeAction = $.cookie("jxymTreeAction"); $("#" + jxymTreeAction + ">a").addClass("action");
                var _img = $("#" + jxymTreeOpenedId + ">img.s");
                if (jxymTreeisOpen == "true") {
                    _img.attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" }).siblings("ul").show();
                    _img.parents().each(function () {
                        $(this).show();
                        $(">img.s", $(this)).attr({ "src": imgOpenedURL, "alt": "折叠", "title": "折叠" });
                    });
                    _img.parent().siblings("li>img.s").attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide();
                } else {
                    _img.attr({ "src": imgClosedURL, "alt": "展开", "title": "展开" }).siblings("ul").hide();
                }
            }
            /*开启cookie保存节点信息*/
            var saveNodeCookie = function (e, isOpen) {
                var id = $(e).parent().attr("id");
                $.cookie("jxymTreeOpenedId", id);
                $.cookie("jxymTreeisOpen", isOpen);
            }
        }
    });
})(jQuery);